"""
项目启动脚本
"""

import os
import sys
import subprocess
from pathlib import Path

def check_requirements():
    """检查依赖是否已安装"""
    try:
        import fastapi
        import uvicorn
        import requests
        import bs4
        import sqlalchemy
        print("✅ 依赖检查通过")
        return True
    except ImportError as e:
        print(f"❌ 缺少依赖: {e}")
        print("请运行: pip install -r requirements.txt")
        return False

def check_env():
    """检查环境配置"""
    env_file = Path(".env")
    if not env_file.exists():
        print("❌ 未找到 .env 文件")
        print("请复制 .env.example 为 .env 并配置相关参数")
        return False
    
    print("✅ 环境配置文件存在")
    return True

def start_server():
    """启动服务器"""
    print("🚀 启动自动文章发送工具...")
    
    try:
        # 设置环境变量
        os.environ["PYTHONPATH"] = str(Path.cwd())
        
        # 启动服务
        cmd = [sys.executable, "-m", "uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
        subprocess.run(cmd)
        
    except KeyboardInterrupt:
        print("\n👋 服务已停止")
    except Exception as e:
        print(f"❌ 启动失败: {e}")

def main():
    """主函数"""
    print("=" * 50)
    print("🤖 自动文章发送工具")
    print("=" * 50)
    
    # 检查依赖
    if not check_requirements():
        return
    
    # 检查环境配置
    if not check_env():
        return
    
    # 启动服务器
    start_server()

if __name__ == "__main__":
    main()